package com.xiguanlezz.cn.server;

import com.xiguanlezz.cn.UserService;
import com.xiguanlezz.cn.service.UserServiceImpl;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.server.TThreadedSelectorServer;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TTransportException;
import org.apache.thrift.transport.layered.TFramedTransport;

/**
 * @Author：习惯了种种
 * @CreateDate：2024/10/27 22:23
 * @Description：主从版Reactor服务器实现
 */
public class ThreadSelectorServer {
    public static void main(String[] args) throws TTransportException {
        // bind本地端口
        TNonblockingServerSocket transport = new TNonblockingServerSocket(8888);

        TFramedTransport.Factory frameFactory = new TFramedTransport.Factory();

        // 协议工厂（方法名、方法参数反序列化，响应结果序列化）
        // TCompactProtocol必须和TFramedTransport结合使用
        TCompactProtocol.Factory protocolFactory = new TCompactProtocol.Factory();

        // 业务实现类
        UserService.Processor processor = new UserService.Processor(new UserServiceImpl());

        TThreadedSelectorServer.Args arg = new TThreadedSelectorServer.Args(transport)
                .protocolFactory(protocolFactory)
                .transportFactory(frameFactory)
                .processor(processor);

        // 启动基于NIO的主从Reactor服务器
        TThreadedSelectorServer threadedSelectorServer = new TThreadedSelectorServer(arg);
        threadedSelectorServer.serve();
    }
}
